package com.probox.common.entity.service.product.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

import com.probox.common.core.constant.CachePrefix;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.exception.ProductException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.redis.service.RedisService;
import com.probox.common.entity.domain.product.ProductInfo;
import com.probox.common.entity.service.product.IProductInfoService;
import com.probox.common.entity.service.product.IProductUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.product.ProductUnitMapper;
import com.probox.common.entity.domain.product.ProductUnit;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品单位Service业务层处理
 *
 *
 * @date 2021-05-27
 */
@Service
@Transactional
public class ProductUnitServiceImpl extends ServiceImpl<ProductUnitMapper, ProductUnit> implements IProductUnitService {

    @Autowired
    private ProductUnitMapper productUnitMapper;

    @Autowired
    private RedisService redisService;


    /**
     * 查询商品单位
     *
     * @param unitUid 商品单位ID
     * @return 商品单位
     */
    @Override
    public ProductUnit selectProductUnitById(Long unitUid) {
        return this.getById(unitUid);
    }

    @Override
    public String getUnitName(Long productUid){
        String name = "";

        return name;
    }

    /**
     * 查询商品单位列表
     *
     * @param productUnit 商品单位
     * @return 商品单位
     */
    @Override
    public List<ProductUnit> queryList(ProductUnit productUnit) {
        QueryWrapper<ProductUnit> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(productUnit.getUnitName())) && productUnit.getUnitName() != null) {
            queryWrapper.like("UNIT_NAME", productUnit.getUnitName());
        }
        if (productUnit.getGroupUid() != null) {
            queryWrapper.eq("group_uid", productUnit.getGroupUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productUnit.getDataStatus())) && productUnit.getDataStatus() != null) {
            queryWrapper.eq("DATA_STATUS", productUnit.getDataStatus());
        }
        Long platformId = SecurityUtils.getPlatformId();
        if (platformId == null) {
            throw new BaseException("当前账号异常");
        }
        queryWrapper.eq("PLATFORM_UID", platformId);
        queryWrapper.ne("DATA_STATUS", DataStatus.delete.getKey());

        if (StringUtils.isNotEmpty(String.valueOf(productUnit.getDataPriority())) && productUnit.getDataPriority() != null) {
            queryWrapper.orderByAsc("DATA_PRIORITY");
        } else {
            queryWrapper.orderByAsc("DATA_PRIORITY");
            queryWrapper.orderByDesc("UPDATE_TIME");
        }

        return productUnitMapper.selectList(queryWrapper);
    }

    /**
     * 新增商品单位
     *
     * @param productUnit 商品单位
     * @return 结果
     */
    @Override
    public boolean insertProductUnit(ProductUnit productUnit) {
        // 名称不能重名
        Integer result = queryName(productUnit.getUnitName(), null);
        if (result > 0) {
           // throw new ProductException("折扣名称不能重名！");
        }
        Long userId = SecurityUtils.getUserId();
        DateTime date = DateUtil.date();
        String username = SecurityUtils.getUsername();
        productUnit.setPlatformUid(SecurityUtils.getPlatformId());
        productUnit.setCreateUid(userId);
        productUnit.setUpdateUid(userId);
        productUnit.setCreateName(username);
        productUnit.setUpdateName(username);
        productUnit.setUpdateTime(date);
        productUnit.setCreateTime(date);
        redisService.deleteObject(CachePrefix.PRODUCT_UNIT_SELECT + SecurityUtils.getPlatformId());
        return this.save(productUnit);

    }

    /**
     * 修改商品单位
     *
     * @param productUnit 商品单位
     * @return 结果
     */
    @Override
    public boolean updateProductUnit(ProductUnit productUnit) {
        // 名称不能重名
        Integer result = queryName(productUnit.getUnitName(), productUnit.getUnitUid());
        if (result > 0) {
           // throw new ProductException("商品单位名称不能重名！");
        }
        productUnit.setPlatformUid(SecurityUtils.getPlatformId());
        productUnit.setUpdateUid(SecurityUtils.getUserId());
        productUnit.setUpdateName(SecurityUtils.getUsername());
        productUnit.setUpdateTime(DateUtil.date());
        redisService.deleteObject(CachePrefix.PRODUCT_UNIT_SELECT + SecurityUtils.getPlatformId());
        return this.updateById(productUnit);
    }

    /**
     * 批量删除商品单位
     *
     * @param unitUids 需要删除的商品单位ID
     * @return 结果
     */
    @Override
    public boolean deleteProductUnitByIds(Long[] unitUids) {
        UpdateWrapper<ProductUnit> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("UNIT_UID", Arrays.asList(unitUids));
        updateWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_UID", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        return this.update(updateWrapper);
    }


    /**
     * 防止重名
     *
     * @param name 名字
     * @param uid  类别ID
     * @return
     */
    public Integer queryName(String name, Long uid) {
        QueryWrapper<ProductUnit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("UNIT_NAME", name);
        queryWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
        queryWrapper.ne("DATA_STATUS", DataStatus.delete.getKey());
        if (uid != null) {
            queryWrapper.ne("UNIT_UID", uid);
        }
        Integer integer = productUnitMapper.selectCount(queryWrapper);
        return integer;
    }

    /**
     * 获取单位的下拉框
     *
     * @return
     */
    @Override
    public List<ProductUnit> bulidSelect() {
        Long platformId = SecurityUtils.getPlatformId();
        if (platformId == null) throw new BaseException("当前账号异常");
        String key = CachePrefix.PRODUCT_UNIT_SELECT + platformId;
        List<ProductUnit> list = redisService.getCacheList(key);
        if (ObjectUtil.isEmpty(list)) {
            QueryWrapper<ProductUnit> queryWrapper = Wrappers.query();
            queryWrapper.eq("PLATFORM_UID", platformId);
            queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
            queryWrapper.orderByAsc("DATA_PRIORITY");
            list = productUnitMapper.selectList(queryWrapper);
            if(ObjectUtil.isNotEmpty(list)) redisService.setCacheList(key, list);
        }
        return list;
    }




}
